package br.com.sgmf.dao;

import br.com.sgmf.model.Peca;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Repository
public class PecaDAO {

    @PersistenceContext
    private EntityManager manager;

    @Transactional
    public void save(Peca peca) {
        manager.persist(peca);
    }

    @Transactional
    public void update(Peca peca) {
        manager.merge(peca);
    }

    @Transactional
    public void delete(Peca peca) {
        manager.remove(peca);
    }

    public Peca findById(Integer id) {
        return manager.find(Peca.class, id);
    }

    @SuppressWarnings("unchecked")
    public List<Peca> findAll() {
        return manager.createQuery("SELECT p FROM Peca p").getResultList();
    }

}
